import pygame
import time
import sys
from pygame.locals import *
pygame.init()
import random
size = width, height = 500, 500
black = 255, 255, 255

roadsize = width, height = 50,40
roadsurf = pygame.Surface(roadsize)
doomsurf = pygame.Surface(roadsize)
screen = pygame.display.set_mode(size)

text = pygame.font.Font(None, 30)

gmap = [[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 1, 0, 0, 1, 1, 1, 1, 0, 0],
        [1, 1, 0, 0, 1, 0, 0, 1, 0, 0],
        [1, 0, 0, 0, 1, 1, 0, 1, 0, 0],
        [1, 1, 1, 0, 0, 1, 0, 1, 1, 0],
        [0, 0, 1, 0, 1, 1, 0, 0, 1, 0],
        [0, 1, 1, 0, 1, 0, 0, 0, 1, 0],
        [0, 1, 0, 0, 1, 0, 0, 0, 1, 1],
        [0, 1, 1, 1, 1, 0, 0, 0, 1, 1]]

gametile = pygame.Rect(0,0,500,400)
scoretile = pygame.Rect(0,400,500,50)
livestile = pygame.Rect(0, 450, 500, 50)
wintile = pygame.Rect(250, 450, 250, 50)
class Player:
    def __init__(self, x, y, lives):
        self.x = x*50
        self.y = y*40
        self.speed = 5
        self.color = 255,0,0
        self.initialx = x*50+25
        self.initialy = y*40+20
        self.lives = lives
        
    def move(self, keys, gamemap, screen):
        (x_temp,y_temp) = (self.x, self.y)
        if keys[K_UP]:
            self.y-=1*self.speed
        elif keys[K_DOWN]:
            self.y+=1*self.speed
        elif keys[K_RIGHT]:
            self.x+=1*self.speed
        elif keys[K_LEFT]:
            self.x-=1*self.speed
        else:
            return (self.x,self.y)

        space = pygame.draw.circle(screen,self.color,(x_temp,y_temp),10)
        pos =  space.center
        
        if gmap[int(pos[1]/40)][int(pos[0]/50)] == 0:
            self.color = 0,0,255
            self.lives -= 1
            self.x = self.initialx
            self.y = self.initialy
        else: self.color = 255,0,0

        if int(self.x/50) == len(gmap) or int(self.y/40) == len(gmap[0]) or int(self.x/50) < 0 or int(self.y/40) < 0:
            self.x = x_temp
            self.y = y_temp
            
        print str(int(pos[1]/40))+'-'+str(int(pos[0]/50))
        return (self.x,self.y)
    
class cookie1:
    def __init__(self, x, y, num):
        self.x = x*50+25
        self.y = y*40+20
        self.color = 100,40,100
        self.num = num
        self.poss = []
        
    def genpos(self):
        temp = []
        for ind, row in enumerate(gmap):
            for tind, t in enumerate(row):
                if t == 1:
                    temp.append((tind*50+25,ind*40+20))
                else:
                    continue
        for i in range(self.num):
            self.poss.append(temp[random.randint(0,len(temp)-1)])
                
class cookie2:
    def __init__(self, x, y, num):
        self.x = x*50+25
        self.y = y*40+20
        self.color = 255,40,255
        self.num = num
        self.poss = []
        
    def genpos(self):
        temp = []
        for ind, row in enumerate(gmap):
            for tind, t in enumerate(row):
                if t == 1:
                    temp.append((tind*50+25,ind*40+20))
                else:
                    continue
        for i in range(self.num):
            self.poss.append(temp[random.randint(0,len(temp)-1)])

clock = pygame.time.Clock()
player = Player(1,0,5)
points = cookie1(1,0,5)
cooks = cookie2(0,0,3)
cooks.genpos()
points.genpos()
created = False
lived = False
score = 0
print points.poss
while 1:
    clock.tick(20)
    keys = pygame.key.get_pressed()
        
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

    if lived == True:
        (x,y) = player.move(keys, gmap, screen)
    else:
        (x,y) = player.initialx, player.initialy
        
    screen.fill(black)    
    for ind, row in enumerate(gmap):
        for tind, t in enumerate(row):
            if t == 1:
                roadsurf.fill((100,255,0))
                screen.blit(roadsurf, pygame.Rect(tind*50,ind*40,50,40))
            else:
                doomsurf.fill((0,0,255))
                screen.blit(doomsurf, pygame.Rect(tind*50,ind*40,50,40))
                
    for cord in cooks.poss:
        pygame.draw.circle(screen,cooks.color,cord,4)

    for cord in points.poss:
        pygame.draw.circle(screen,points.color,(cord),3)

    
    for point in points.poss:
        if pygame.draw.circle(screen,player.color,(x,y),10).collidepoint(point):
            points.poss.remove(point)
            score += 5
        else:
            continue
    for cook in cooks.poss:
        if pygame.draw.circle(screen,player.color,(x,y),10).collidepoint(cook):
            cooks.poss.remove(cook)
            score += 10
        else:
            continue
    if lived == False:
        screen.blit(text.render("Press a number key to set lives [1-9]",1,(0,0,0)),livestile)
        if keys[K_1]:
            player.lives = 1
            lived = True
            pygame.display.flip()
            continue
        if keys[K_2]:
            player.lives = 2
            lived = True
            pygame.display.flip()
            continue
        if keys[K_3]:
            player.lives = 3
            lived = True
            pygame.display.flip()
            continue
        if keys[K_4]:
            player.lives = 4
            lived = True
            pygame.display.flip()
            continue
        if keys[K_5]:
            player.lives = 5
            lived = True
            pygame.display.flip()
            continue
        if keys[K_6]:
            player.lives = 6
            lived = True
            pygame.display.flip()
            continue
        if keys[K_7]:
            player.lives = 7
            lived = True
            pygame.display.flip()
            continue
        if keys[K_8]:
            player.lives = 8
            lived = True
            pygame.display.flip()
            continue
        if keys[K_9]:
            player.lives = 9
            lived = True
            pygame.display.flip()
            continue
    else:
        screen.blit(text.render("Lives: " + str(player.lives),1,(0,0,255)),livestile)
    screen.blit(text.render("Score: " + str(score),1,(0,0,255)), scoretile)
    pygame.draw.circle(screen,player.color,(x,y),10)
    if created == False:
        pygame.draw.circle(screen,player.color,(player.initialx,player.initialy),10)
        player.x = player.initialx
        player.y = player.initialy
        created = True
        pygame.display.flip()
        continue
    pygame.display.flip()
    if player.lives == 0:
        screen.blit(text.render("LOSER!!!!!",1,(255,0,0)),wintile)
        pygame.display.flip()
        break
    if len(cooks.poss) == 0 and len(points.poss) == 0:
        screen.blit(text.render("YOU WIN!!!!!",1,(0,255,255)),wintile)
        pygame.display.flip()
        break
    
pygame.quit()
